﻿using Company.Database.Entities;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Company.Database.Sqlite
{
    /* 第一步，安装 Microsoft.EntityFrameworkCore.Sqlite 6.0.26
    * 第二步，安装  Microsoft.EntityFrameworkCore.Tools 6.0.26
    * 第三步，创建实体和DbContext子类安装
    * 第四步，在程序包管理器控制台执行下列命令
    * Add-Migration InitialCreate
    * Update-Database
    * 
    * 注意事项：Sqlite数据文件的地址因为权限问题，最好放到下面这个地方，
    * var folder = Environment.SpecialFolder.LocalApplicationData;
    * var path = Environment.GetFolderPath(folder);
    * DbPath = System.IO.Path.Join(path, "Application.db");
    * 
    * 否则，使用Update-Database之后，将生成的DB文件覆盖到程序根目录
    * DbPath = System.IO.Path.Join(Environment.CurrentDirectory, "Application.db");
    * 
    * 
    */


    class SqliteDbContext : DbContext
    {
        public DbSet<User> Users { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            base.OnConfiguring(optionsBuilder);

            var local = Environment.SpecialFolder.LocalApplicationData;
            var path = Environment.GetFolderPath(local);
            var fullname = System.IO.Path.Join(path, "sqlite_demo.db");
            optionsBuilder.UseSqlite($"Data Source = {fullname}");
        }
    }
}
